package com.leapp.sk.viewdelegate.personal

import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.SKTopBar
import com.example.mvp.view.WindowBottomDialog
import com.leapp.sk.R
import com.leapp.sk.inter.ChangerSexInterface
import com.leapp.sk.ui.activity.personal.PersonalDataActivity

/**
 * 个人资料视图托管类
 * @author  chaohaipeng  on 2018/8/8.
 */
class PersonalDataDelegate :BaseAppDelegate(){
    /**
     * 图片加载管理
     */
    var mRequestManager: RequestManager? = null

    /**
     * 图片加载操作
     */
    var mRequestOptions: RequestOptions = RequestOptions()


    /**
     * 底部弹出Dialog
     */
    private var windowBottomDialog: WindowBottomDialog? = null
    /**
     * 底部弹出View
     */
    private var bottomView: View? = null

    /**
     * 第一个选项文本
     */
    private var tv_dailog_bottom_frist_text: TextView? = null
    /**
     * 第二个选项文本
     */
    private var tv_dailog_bottom_second_text: TextView? = null
    /**
     * 取消选项文本
     */
    private var tv_dailog_bottom_cancel_text: TextView? = null

    /**
     * 标题
     */
    private var tv_dailog_bottom_title_text: TextView? = null


    private  var changerSexInterface: ChangerSexInterface?=null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_personal_data
    }

    /**
     * 初始化底部弹出
     */
    fun initBottomDialog() {
        if (windowBottomDialog == null) {
            windowBottomDialog = WindowBottomDialog(getActivity(), 0)
        }
    }

    /**
     * 初始化底部弹出视图
     */
    fun initBottomDialogView() {
        if (bottomView == null) {
            bottomView = View.inflate(getActivity(), R.layout.layout_bottom_dialog, null)
            tv_dailog_bottom_frist_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_frist_text)
            tv_dailog_bottom_second_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_second_text)
            tv_dailog_bottom_cancel_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_cancel_text)
            tv_dailog_bottom_title_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_title_text)

        }
    }

    /**
     * 设置头像
     */
    fun setHeadData(path: String?) {
        val ima_apd_avatar = getView<ImageView>(R.id.ima_apd_avatar)
        mRequestManager = Glide.with(getActivity<AppCompatActivity>())
        mRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
        mRequestOptions.skipMemoryCache(true)
        mRequestOptions.error(R.mipmap.icon_defout_head)
        mRequestManager?.load(path)
                ?.thumbnail(0.2f)
                ?.apply(mRequestOptions)
                ?.into(ima_apd_avatar)
    }

    /**
     * 初始化性别底部弹出窗
     */
    fun initSexDialog( changerSexInterface: ChangerSexInterface) {
        this.changerSexInterface=changerSexInterface
        initBottomDialog()
        initBottomDialogView()
        tv_dailog_bottom_frist_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_personal_dialog_male)
        tv_dailog_bottom_second_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_personal_dialog_female)
        tv_dailog_bottom_title_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_personal_dialog_sex_title)
        windowBottomDialog?.setBottomView(bottomView)
        windowBottomDialog?.show()
        initEvent(true)
    }


    /**
     * 初始化事件
     */
    fun initEvent(isSex: Boolean) {
        initBottomDialog()
        initBottomDialogView()
        tv_dailog_bottom_frist_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
                setSexData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_personal_dialog_male))
            changerSexInterface?.getSex(1)
        })
        tv_dailog_bottom_second_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
                setSexData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_personal_dialog_female))
            changerSexInterface?.getSex(0)
        })
        tv_dailog_bottom_cancel_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
        })
    }


    /**
     * 昵称获取
     */
    fun getNick():String {
        val tv_apd_nick = getView<TextView>(R.id.tv_apd_nick)
        val trim = tv_apd_nick.text.toString().trim()
            return trim
    }

    /**
     * 性别
     */
    fun getsex():Int {
        val tv_apui_sex = getView<TextView>(R.id.rl_apd_personal_sex)
        val trim = tv_apui_sex.text.toString().trim()
        if (TextUtils.equals(trim,getActivity<AppCompatActivity>().resources?.getString(R.string.string_personal_dialog_male))){
            return 1
        }else if (TextUtils.equals(trim,getActivity<AppCompatActivity>().resources?.getString(R.string.string_personal_dialog_female))){
            return 0
        }
        return 0
    }


    /**
     * 设置手机号
     */
    fun setPhone(nick:String?){
        val tv_apd_personal_phone = getView<TextView>(R.id.tv_apd_personal_phone)
        if (!TextUtils.isEmpty(nick)){
            tv_apd_personal_phone.text=nick
        }
    }
    /**
     * 设置手机号
     */
    fun getPhone():String{
        val tv_apd_personal_phone = getView<TextView>(R.id.tv_apd_personal_phone)
        val trim = tv_apd_personal_phone.text.toString().trim()
        return trim
    }
    /**
     * 设置个人真实姓名
     */
    fun setName(name:String?){
        val tv_apd_personal_name = getView<TextView>(R.id.tv_apd_personal_name)
        if (!TextUtils.isEmpty(name)){
            tv_apd_personal_name.text=name
        }
    }
    /**
     * 设置个人昵称
     */
    fun setNick(nick:String?){
        val tv_apd_nick = getView<TextView>(R.id.tv_apd_nick)
        if (!TextUtils.isEmpty(nick)){
            tv_apd_nick.text=nick
        }
    }
    /**
     * 获取个人介绍
     */
    fun getIntroduce():String {
        val tv_apd_personal_introduce = getView<TextView>(R.id.tv_apd_personal_introduce)
        val trim = tv_apd_personal_introduce.text.toString().trim()
        return trim
    }
    /**
     * 设置个人介绍
     */
    fun setIntroduce(introduce:String?){
        val tv_apd_personal_introduce = getView<TextView>(R.id.tv_apd_personal_introduce)
        if (!TextUtils.isEmpty(introduce)){
            tv_apd_personal_introduce.text=introduce
        }
    }
    /**
     * 设置性别数据
     */
     fun setSexData(content: String?) {
        val tv_apd_personal_sex = getView<TextView>(R.id.tv_apd_personal_sex)
        if (!TextUtils.isEmpty(content)) {
            tv_apd_personal_sex.text = content
        }
    }

    /**
     * 设置生日
     */
    fun setBrith(content: String?) {
        val tv_apd_personal_brith = getView<TextView>(R.id.tv_apd_personal_brith)
        if (!TextUtils.isEmpty(content)) {
            tv_apd_personal_brith.text = content
        }
    }
    /**
     * 设置生日
     */
    fun getBrith():String {
        val tv_apd_personal_brith = getView<TextView>(R.id.tv_apd_personal_brith)
        val trim = tv_apd_personal_brith.text.toString().trim()
        return trim
    }
    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_apd= getView<SKTopBar>(R.id.jtb_apd)
        if (!TextUtils.isEmpty(title)){
            jtb_apd.setTitle(title)
        }
        setEvent(jtb_apd)
    }

    /**
     * 设置设置事件
     */
    fun setEvent(topBar:SKTopBar){
        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<PersonalDataActivity>().finish()

            }
            override fun rightOnClick() {
            }

        })
    }




}